package com.lokyiu.mybible.service.impl;

//import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.lokyiu.mybible.domain.BibleBook;
import com.lokyiu.mybible.domain.Chapter;
import com.lokyiu.mybible.domain.Verse;
import com.lokyiu.mybible.service.BibleBookService;
import com.lokyiu.mybible.service.ChapterService;
import com.lokyiu.mybible.service.ContentService;
import com.lokyiu.mybible.service.VerseService;
import com.lokyiu.mybible.wol.ContentDownloader;
import com.lokyiu.mybible.wol.UrlDocumentFactory;
import com.lokyiu.mybible.wol.WolLanguage;

public class ContentServiceImpl extends AbstractAndroidService implements ContentService {
	

	public ContentServiceImpl(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public ContentServiceImpl(SQLiteDatabase db) {
		super(db);
	}
	
	private BibleBookService bookService;
	public void setBookService(BibleBookService bookService) {
		this.bookService = bookService;
	}
	
	private ChapterService chapterService;
	@Override
	public void setChapterService(ChapterService chapterService) {
		this.chapterService = chapterService;		
	}
	
	private VerseService verseService;
	@Override
	public void setVerseService(VerseService verseService) {
		this.verseService = verseService;		
	}

	@Override
	public void getContent(WolLanguage language) throws Exception {
		//Create three lists (One for book, chapter and verse)
		List<BibleBook> books = new LinkedList<BibleBook>();
		List<Chapter> chapters = new LinkedList<Chapter>();
		List<Verse> verses =new LinkedList<Verse>();
		
		//Use ContentDownloader class to get list of bible books - assign to bible book list
		
		UrlDocumentFactory urlDf = new UrlDocumentFactory(language.getUrl());
		books.addAll(new ContentDownloader().getBibleBooks(urlDf));
		
		//for each book, use ContentDownloader to get chapters - add to chapter list

		
		for(BibleBook book : books){
			UrlDocumentFactory bookURL = new UrlDocumentFactory(book.getUrl());
			chapters.addAll(new ContentDownloader().getChapters(bookURL, book));
		}
	
		//for each chapter, use ContentDownloader to get verses - add to verse list
		
		for(Chapter chapter : chapters){
			UrlDocumentFactory chapterURL = new UrlDocumentFactory(chapter.getUrl());
			verses.addAll(new ContentDownloader().getVerses(chapterURL, chapter));
		}
		
		BibleBookServiceImpl bookService = new BibleBookServiceImpl(getWritableDatabase());
		setBookService(bookService);
		bookService.save(books);
		
		ChapterServiceImpl chapterService = new ChapterServiceImpl(getWritableDatabase());
		setChapterService(chapterService);
		chapterService.save(chapters);
		
		VerseServiceImpl verseService = new VerseServiceImpl(getWritableDatabase());
		setVerseService(verseService);
		verseService.save(verses);
		

		//All three lists have been populated with the complete book, chapter and verse content.
		//Now use BibleBookService, ChapterService and VerseService to save them.
	}
	
	
}
